﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;

/// <summary>
/// Summary description for ClaimBLL
/// </summary>
public class ClaimBLL
{
     ClaimDAL dal = null;
     AccountsManager am = new AccountsManager(); 

    

	public ClaimBLL()
	{
		 dal = new ClaimDAL();
	}
    public DataSet getClaimList()
    {

        return dal.getPendingClaimList();
    }
       
   
    public void newClaim(   String accountNo,
                            String claimDescription,
                            String claimStatus,
                            decimal claimAmount,
                            DateTime claimDate,
                            String staffID,
                            int insuredID,
                            String docCategory1,
                            String docPath1,
                            String docCategory2,
                            String docPath2,
                            String docCategory3,
                            String docPath3,
                            String docCategory4,
                            String docPath4,
                            String docCategory5,
                            String docPath5,
                            String docCategory6,
                            String docPath6)
    {
        dal.insertClaim( accountNo,
                        claimDescription,
                        claimStatus,
                        claimAmount,
                        claimDate,
                        insuredID,
                        staffID,
                        docCategory1,
                        docPath1,
                        docCategory2,
                        docPath2,
                        docCategory3,
                        docPath3,
                        docCategory4,
                        docPath4,
                        docCategory5,
                        docPath5,
                        docCategory6,
                        docPath6);
    }


    public System.Collections.Hashtable getClaimInfo(int insuredID)
    {
        Hashtable ht = new Hashtable();
        DataSet ds = dal.getClaimApplyInfo(insuredID);
        if (ds.Tables["docsTab"].Rows.Count > 0)
        {
            String planType = ds.Tables["docsTab"].Rows[0]["planType"] as String;
            String accountNo = ds.Tables["docsTab"].Rows[0]["accountNo"] as String;
            int caseID = (int)ds.Tables["docsTab"].Rows[0]["caseID"];
            String claimStatus = ds.Tables["docsTab"].Rows[0]["claimStatus"] as String;
            String claimAmount = ds.Tables["docsTab"].Rows[0]["claimAmount"] as String;
            String claimDate = ds.Tables["docsTab"].Rows[0]["claimDate"] as String;
            String staffID = ds.Tables["docsTab"].Rows[0]["staffID"] as String;
            ht.Add("planType", planType);
            ht.Add("accountNo", accountNo);
            ht.Add("caseID", caseID);
            ht.Add("claimStatus", claimStatus);
            ht.Add("claimDate", claimDate);
            ht.Add("staffID", staffID);

            String docCategory1 = ds.Tables["docsTab"].Rows[0]["docCategory1"] as String;
            String docCategory2 = ds.Tables["docsTab"].Rows[0]["docCategory2"] as String;
            String docCategory3 = ds.Tables["docsTab"].Rows[0]["docCategory3"] as String;
            String docCategory4 = ds.Tables["docsTab"].Rows[0]["docCategory4"] as String;
            String docCategory5 = ds.Tables["docsTab"].Rows[0]["docCategory5"] as String;
            String docCategory6 = ds.Tables["docsTab"].Rows[0]["docCategory6"] as String;
            
            ht.Add("docCategory1", docCategory1);
            ht.Add("docCategory2", docCategory2);
            ht.Add("docCategory3", docCategory3);
            ht.Add("docCategory4", docCategory4);
            ht.Add("docCategory5", docCategory5);
            ht.Add("docCategory6", docCategory6);

            String docPath1 = ds.Tables["docsTab"].Rows[0]["docPath1"] as String;
            String docPath2 = ds.Tables["docsTab"].Rows[0]["docPath2"] as String;
            String docPath3 = ds.Tables["docsTab"].Rows[0]["docPath3"] as String;
            String docPath4 = ds.Tables["docsTab"].Rows[0]["docPath4"] as String;
            String docPath5 = ds.Tables["docsTab"].Rows[0]["docPath5"] as String;
            String docPath6 = ds.Tables["docsTab"].Rows[0]["docPath6"] as String;

            ht.Add("docPath1", docPath1);
            ht.Add("docPath2", docPath2);
            ht.Add("docPath3", docPath3);
            ht.Add("docPath4", docPath4);
            ht.Add("docPath5", docPath5);
            ht.Add("docPath6", docPath6);
         

        }




        return ht;
    }

    


    public void confirm(String planType, int insuredID, int caseId, decimal amount, String staffID, String claimDescription)
    {
        decimal limit = 0;
        decimal total = dal.getTotalClaimAmount(insuredID) + amount;
        TerminateDAL tdal = new TerminateDAL();

        if("A".Equals(planType)){
            limit = 70000;

        }else if("B".Equals(planType)){
            limit = 55000;

        }else if("C".Equals(planType)){
            limit =40000;

        }else if("D".Equals(planType)){
            limit = 70000;

        }else if("E".Equals(planType)){
            limit = 40000;
        }
        else if ("F".Equals(planType))
        {
            limit = 25000;
        }
        else
        {
            limit = 1000000;
           
        }

        
        if (total <= limit)
        {
            dal.confirm(caseId, amount, staffID, claimDescription);
            Hashtable ht = am.getInsuranceInfo(insuredID);
            String custID = ht["custID"] as String;

            String savingAccountNo = am.getInsuranceSavingAccountNo(custID);
            if (limit == 1000000)
            {
                tdal.terminate(insuredID, "Personal Accident Claimed.");

            }
            else
            {
                dal.transferClaimAmount(savingAccountNo, amount);

            }
           
        }
        else
        {
            throw new Exception("Over-Limit");

        }
     
    }

    public void denied(String planType, int insuredID, int caseId, decimal amount, String staffID, String claimDescription)
    {
        dal.denied(caseId, amount, staffID, claimDescription);

    }

}